// 文件名编码方式为 7bit，导入模块还原 文件中文名称
const Buffer = require("buffer").Buffer;
const fs = require("fs");
const path = require("path");
// 导入处理密码的模块
const { sourcePath } = require("../config");
const { updateAvatarApi } = require('../request/api/master')

//
//
// 上传文件，包括图片、音频、
exports.uploadFile = (req, res) => {
  const { originalname, filename, path: path_, size, mimetype } = req.file;
  const originalFilename = Buffer.from(originalname, "binary").toString();
  // console.log(req.file)

  const fixedUrl = (sourcePath + path_).replace(/\\/g, "/");
  const obj = {
    filename,
    originName: originalFilename,
    path: fixedUrl,
    size,
    type: mimetype,
  };

  return res.cc("上传文件成功！");
};

//
//
// 上传头像
exports.updateAvatar = async (req, res) => {
  const { originalname, filename, path: path_, size, mimetype } = req.file;
  const originalFilename = Buffer.from(originalname, "binary").toString();


  // const fixedUrl = (sourcePath + path_).replace(/\\/g, "/");
  const fixedUrl = (path_).replace(/\\/g, "/");

  const obj = {
    filename,
    originName: originalFilename,
    type: mimetype,
    path: fixedUrl,
    size,
  };

  const file = obj
  const result = await updateAvatarApi(file)
  if (result.code !== 200) return res.cc('更新头像失败！')

  return res.send({
    code: 200,
    data: obj,
    message: "更新头像成功！",
  });
};
